// 客户模糊查询
import React from 'react';
import { Select, Spin } from 'antd';
import { connect } from 'dva';
import HOCSelect from '@/components/AntDesign/Select';

const { Option } = Select;

@connect(({ publicdata: { contList, contFsValue }, loading }) => ({
  contList,
  contFsValue,
  loading: loading.effects['publicdata/fetchContList'],
}))
class Customer extends React.Component {
  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'publicdata/updateModelData',
      payload: { contList: [] },
    });
  }

  // 获取合同
  search = value => {
    const { dispatch, params = {} } = this.props;
    //  params={{ contType: 3 }} ： 查询所有合同
    if (value.trim().length) {
      dispatch({
        type: 'publicdata/fetchContList',
        payload: { contName: value.trim(), ...params },
      });
    }
  };

  getData = () => {};

  // 更改当前选择的值并供给父组件用
  triggerChange(changeValue, options) {
    const { onChange, dispatch, ...rest } = this.props;
    if (onChange) {
      onChange(changeValue, options);
    }
    dispatch({
      type: 'publicdata/updateModelData',
      payload: {
        contFsValue: rest.labelInValue ? { key: options.key, label: options.title } : options,
      },
    });
  }

  render() {
    const { contList = [], loading, getData = this.getData, value, contFsValue, ...rest } = this.props;
    return (
      <HOCSelect
        placeholder="请输入关键字检索"
        {...rest}
        showSearch
        value={value ? (rest.labelInValue && contFsValue) || contFsValue.title : value}
        optionFilterProp="children"
        showArrow={false}
        onSearch={this.search}
        onSelect={(selectValue, options) => getData(selectValue, options)}
        notFoundContent={loading ? <Spin size="small" /> : '暂无数据'}
        onChange={(changeValue, options) => this.triggerChange(changeValue, options)}
      >
        {contList.map(item => (
          <Option key={item.contId} title={item.contName}>
            {item.contName}
          </Option>
        ))}
      </HOCSelect>
    );
  }
}
export default Customer;
